package reflect;

import java.util.Scanner;

/**
 * 反射机制实例化对象
 */
public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Person p = new Person();
        System.out.println(p);

        //需求:利用反射机制实例化Person对象
        /*
            步骤
            1:获取要实例化对象的类对象
            2:通过类对象的方法:newInstance来使用无参构造器实例化对象
         */
        //1
//        Class cls = Class.forName("reflect.Person");

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入类名");
        String className = scanner.nextLine();
        Class cls = Class.forName(className);

        //2
        Object obj = cls.newInstance();//Person p = new Person();
        System.out.println(obj);
    }
}







